package com.twu.biblioteca;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/21/13
 * Time: 6:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class User {
    private static int User_ID_Key = 1111111;

    private String name;
    private String phoneNumber;
    private String emailAddress;
    private String userName;
    private String password;

    public User(String name, String phoneNumber, String emailAddress, String password) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.emailAddress = emailAddress;
        this.userName = generateUserName();
        this.password = password;
    }

    private String generateUserName() {
        String libraryNumber = Integer.toString(User_ID_Key++);
        StringBuffer buffer = new StringBuffer(libraryNumber);
        buffer.insert(3, "-");
        return buffer.toString();
    }

    public String toString() {
        return name + "\t" + phoneNumber + "\t" + emailAddress;
    }

    public static User authenticate(String username, String password) {
        for(User user : BibliotecaModel.getUsers()) {
            if(user.userName.equals(username) && user.password.equals(password)) {
                return user;
            }
        }
        return null;
    }

    public String getUserName() {
        return userName;
    }
}
